package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ArticleManagement;
import com.ruoyi.system.domain.PageArtcle;
import com.ruoyi.system.domain.ShopClassProductDetail;
import com.ruoyi.system.service.ArticleService;
import com.ruoyi.system.service.ShopClassProductDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/***
 * 说明   文章管理相关接口
 * @author:ZZ
 * @date: 2024-12-05  17:26
 **/
@RestController
@RequestMapping("/system/article")
@Api(tags = "文章管理相关接口")
public class ArticleController extends BaseController {

      @Autowired
    private ArticleService service;


      @GetMapping("/list")
      @ApiOperation("文章信息列表,带分页  带 根据文章标题名称搜索   包含上架 / 下架所有状态 ")
      public Object list( @RequestParam(value = "pageNum",required = false)Integer pageNum,
                          @RequestParam(value = "pageSize",required = false)Integer pageSize,
                          @RequestParam(value = "fileName",required = false)String fileName){
          if (pageNum == null || pageSize == null){
              return error("参数不能为空 ！");
          }
           PageHelper.startPage(pageNum, pageSize);
          QueryWrapper<ArticleManagement> wrapper = new QueryWrapper<>();
          wrapper.like(fileName !=null && !fileName.isEmpty(),"title",fileName);
          List<ArticleManagement> list = service.list(wrapper);
          PageInfo<ArticleManagement> of = PageInfo.of(list);
          return  success(of);
      }

      @GetMapping("/listDetail")
      @ApiOperation("文章信息列表,带分页  带 根据文章标题名称搜索   获取所有状态为上架的  ")
      public Object listDetail( @RequestParam(value = "pageNum",required = false)Integer pageNum,
                          @RequestParam(value = "pageSize",required = false)Integer pageSize,
                          @RequestParam(value = "fileName",required = false)String fileName){
          if (pageNum == null || pageSize == null){
              return error("参数不能为空 ！");
          }
           PageHelper.startPage(pageNum, pageSize);
          QueryWrapper<ArticleManagement> wrapper = new QueryWrapper<>();
          wrapper.like(fileName !=null && !fileName.isEmpty(),"title",fileName);
          wrapper.eq("frame",0);
          List<ArticleManagement> list = service.list(wrapper);
          PageInfo<ArticleManagement> of = PageInfo.of(list);
          return  success(of);
      }

      @PostMapping("/save")
      @ApiOperation("保存文章的相关信息接口")
    public Object saveArticle( ArticleManagement management) throws IOException {
          if (management == null){
              return error("参数不能为空 ！");
          }
          Object o = service.saveArticle(management);
          return o;
      }


    /**
     * 上架 下架 状态修改
     *  是否上下架（1--上架2--下架）
     * */
    @PutMapping("shelvesModified")
    @ApiOperation("文章上下架")
    public AjaxResult shelvesModified( PageArtcle detail){
        if(null == detail){
            return success("参数不能为空");
        }
        UpdateWrapper<ArticleManagement> wrapper = new UpdateWrapper<>();
        wrapper.set("frame",detail.getFrame());
        wrapper.eq("id",detail.getId());
        boolean flag = service.update(wrapper);
        return flag ?success("操作成功"):success("操作失败");
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除指定数据")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable("ids") List<Long> ids) {
        if (ids == null || ids.size()==0){
            return success("参数不能为空");
        }
          service.removeBatchByIds(ids);
        return  success("删除成功");
    }


    @Autowired
    private ShopClassProductDetailService shopClassProductDetailService;

    //获取单条数据详情
    @GetMapping(value = "{id}")
    @ApiOperation("查看指定文章详情信息")
    public AjaxResult selectOne(@PathVariable("id") Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        QueryWrapper<ArticleManagement> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq( id != null,"id",id);
        ArticleManagement detail = service.getOne(wrapper);
        return success(detail);
    }




    //获取单条数据详情
    @GetMapping("selectOneByClassName")
    @ApiOperation("查看指定类名下的 文章详情信息 （前端页面调用，除文章详情外还带有热门商品和推荐商品集合）")
    public AjaxResult selectOneByClassName(@RequestParam("id") Long id,@RequestParam("className") String className) {
        HashMap<Object, Object> map = new HashMap<>();
        QueryWrapper<ArticleManagement> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq( id != null,"id",id);
        ArticleManagement detail = service.getOne(wrapper);
        map.put("list",detail);
        QueryWrapper<ShopClassProductDetail> wrapperShop = new QueryWrapper<>();
        wrapperShop.like(null !=className && !className.isEmpty(),"class_name", className);
        List<ShopClassProductDetail> resultList = shopClassProductDetailService.list(wrapperShop);
        map.put("Result", resultList);
        wrapperShop = new QueryWrapper<>();
        wrapperShop.orderByDesc("number_searches");
        List<ShopClassProductDetail> popularProductsList = shopClassProductDetailService.list(wrapperShop);
        List<ShopClassProductDetail> firstEight = popularProductsList.subList(0, Math.min(8, popularProductsList.size()));
        //只获取前八个
        map.put("PopularProducts", firstEight);
        return success(detail);
    }

    /**
     * 修改数据
     *
     * @param management 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(ArticleManagement management) throws IOException {
        if (null != management && management.getTitle() != null) {
        service.updateByDetail(management);
        return  success("修改成功");
        }else {
            return error("参数不能为空");
        }
    }




}
